package com.cade.agriculturemall.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cade.agriculturemall.model.LoginEntity;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.network.NetBroadcastReceiver;
import com.cade.agriculturemall.util.ToastUtils;
import com.cade.agriculturemall.view.main.MainActivity;
import com.cade.agriculturemall.view.retrievePassword.RetrievePasswordActivity;
import com.cade.agriculturemall.view.login.LoginContract.IPresenter;
import com.cade.agriculturemall.view.register.RegisterActivity;

public class LoginActivity extends BaseActivity<LoginContract.IPresenter> implements LoginContract.IView, View.OnClickListener {
    private Button mBtnLogin;
    private Button mBtnForget;
    private Button mBtnRegister;
    private String mPsw;
    private String mPhone;
    private LoginEntity mEntity = new LoginEntity();
    private EditText mEtPhone;
    private EditText mEtPsw;
    private boolean goBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        goBack = getIntent().getBooleanExtra("goBack", false);
    }

    @Override
    public void setupView() {
        mBtnLogin = (Button) findViewById(R.id.login_btn_go);
        mBtnForget = (Button) findViewById(R.id.login_btn_forget);
        mBtnRegister = (Button) findViewById(R.id.login_btn_register);
        mEtPhone = (EditText) findViewById(R.id.login_et_telephone);
        mEtPsw = (EditText) findViewById(R.id.login_et_password);

        mBtnLogin.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
        mBtnForget.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn_go:
                mPsw = mEtPsw.getText().toString();
                mPhone = mEtPhone.getText().toString();
                collectInfo(mPhone, mPsw);
                break;
            case R.id.login_btn_forget:
                Intent intentForget = new Intent(this, RetrievePasswordActivity.class);
                startActivity(intentForget);
                break;
            case R.id.login_btn_register:
                Intent intentRegister = new Intent(this, RegisterActivity.class);
                startActivity(intentRegister);
                break;
        }
    }


    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {
    }

    @Override
    public IPresenter setupPresenter() {
        return new LoginPresenter(this);
    }


    @Override
    public void collectInfo(String phone, String psw) {
        if (!TextUtils.isEmpty(phone)) {
            if (!TextUtils.isEmpty(psw)) {
                mEntity.setPhoneNumber(phone.toString().replaceAll(" ", ""));
                mEntity.setPassWord(psw.toString().replaceAll(" ", ""));
                mPresenter.login(mEntity.getPhoneNumber(), mEntity.getPassWord());
            }
        } else {
            ToastUtils.showToast("输入手机号码");
        }
    }
//  goBack==false  已登录，退出登录跳转至此
//    goBack==true   未登录，登录后返回原页面
    @Override
    public void redirection() {
        if (!goBack) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            finish();
        }
    }
////网络状态监听
//    private NetBroadcastReceiver.OnNetWorkChange onNetWorkChange = new NetBroadcastReceiver.OnNetWorkChange() {
//        @Override
//        public void onChange(boolean isConnect) {
//
//        }
//    };
//    //注册监听网络变化
//    private void registerReceiver() {
//        NetBroadcastReceiver.getInstance().setOnNetWorkChange(onNetWorkChange);
//    }
//
//    //取消监听网络变化
//    private void unregisterReceiver() {
//        NetBroadcastReceiver.getInstance().delOnNetWorkChange(onNetWorkChange);
//    }
}
